package com.liujg.future;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

import static com.liujg.future.ReadCodeFromWebSync.readHttps;

public class ProvinceCityScraper {
    public static void main(String[] args) {
        String url = "https://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2023/index.html";

        try {
            try {
                readHttps();
            }catch (Exception e){
                e.printStackTrace();
            }

            // 连接到网页并获取其内容
            Document doc = Jsoup.connect(url)
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
                    .get();

            // 查找包含省份信息的部分
            Elements provinceElements = doc.select("tr.provincetr > td > a");

            for (Element province : provinceElements) {
                System.out.println("省份: " + province.text());

                // 获取每个省份对应的链接
                String provinceUrl = "https://www.stats.gov.cn" + province.attr("href");

                // 访问省份页面
                Document provinceDoc = Jsoup.connect(provinceUrl)
                        .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
                        .get();

                // 提取市和区县信息
                Elements cityElements = provinceDoc.select("tr.citytr > td > a");
                for (Element city : cityElements) {
                    System.out.println("  市: " + city.text());

                    String cityUrl = "https://www.stats.gov.cn" + city.attr("href");
                    Document cityDoc = Jsoup.connect(cityUrl)
                            .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
                            .get();

                    Elements districtElements = cityDoc.select("tr.counttr > td > a");
                    for (Element district : districtElements) {
                        System.out.println("    区/县: " + district.text());
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
